﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QLTuyenXe.BLL;
using QLTuyenXe.DAO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Data.SqlClient;
using QLTuyenXe.library;
using Microsoft.Win32;
using System.Threading;

namespace QLTuyenXe
{   
    public partial class frmDangNhap : DevComponents.DotNetBar.Office2007Form
    {
        BUser user = new BUser();
        XuLyDL xl = new XuLyDL();
        ThongBao thongBao = new ThongBao();
        public frmDangNhap()
        {
            Thread t = new Thread(new ThreadStart(khoiDong));
            t.Start();
            Thread.Sleep(5000);
            InitializeComponent();
            t.Abort();
        }
         private void khoiDong()
        {
            Application.Run(new frmKhoiDong());
        }

        public void DangNhap()
        {
        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if ((txtTenDangNhap.Text != "") && (txtPassword.Text != "") && xl.TenDN(txtTenDangNhap.Text) == true)
            {
                DataTable dn = user.Select_DN("name='" + txtTenDangNhap.Text + "'", "password='" + xl.GetMD5(txtPassword.Text) + "'");
                if (dn.Rows.Count > 0)
                {
                    
                    Hide();

                    frmMain main = new frmMain(txtTenDangNhap.Text);
<<<<<<< .mine
                    
                    //main.Show();
                    main.ShowDialog();
                    this.Close();
=======
                   // main.Show();
                    main.ShowDialog();
>>>>>>> .r21
                }
                else
                {
                    if (MessageBoxEx.Show("Bạn nhập sai tên đăng nhập hoặc mật khẩu. \nBạn có muốn đăng nhập lại không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        txtTenDangNhap.Focus();
                    }
                    else
                    {
                        Close();
                        System.Windows.Forms.Application.Exit();
                    }
                }
            }
            else
            {
                thongBao.KiemTraSo();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBoxEx.Show("Bạn có muốn thoát hay không  ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                Close();
                System.Windows.Forms.Application.Exit();
            } 
        }

        //luu pass
        private void LoadRegistry()
        {
            txtTenDangNhap.Text = (string)Registry.GetValue("HKEY_CURRENT_USER\\CWRS ", "UserName", txtTenDangNhap.Text);
            if ((string)Registry.GetValue("HKEY_CURRENT_USER\\CWR S", "Rem", "Yes") == "Yes")
            {
                ckbMatkhau.Checked = true;
                txtPassword.Text = (string)Registry.GetValue("HKEY_CURRENT_USER\\CWRS ", "Password", txtPassword.Text);
            }
        }

        private void SaveRegistry()
        {
            Registry.SetValue("HKEY_CURRENT_USER\\CWRS", "UserName", txtTenDangNhap.Text);
            Registry.SetValue("HKEY_CURRENT_USER\\CWRS", "Password", txtPassword.Text);
            if (ckbMatkhau.Checked)
            {
                Registry.SetValue("HKEY_CURRENT_USER\\CWRS", "Rem", "Yes");
            }
            else
            {
                Registry.SetValue("HKEY_CURRENT_USER\\CWRS", "Rem", "No");
            }
        }

        private void frmDangNhap_Load(object sender, EventArgs e)
        {
            //LoadRegistry();
        }

        private void frmDangNhap_KeyDown(object sender, KeyEventArgs e)
        {
            
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnDangNhap_Click(sender, e);
            }
        }

    }
}
